package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentCategory;
import com.mxpio.erp.common.equipment.service.EquipmentCategoryService;
import com.mxpio.erp.common.equipment.service.EquipmentReportService;
import com.mxpio.erp.common.equipment.vo.EquipmentReportVO;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.jpa.query.SimpleCriterion;
import com.mxpioframework.security.entity.Dept;
import com.mxpioframework.security.service.DeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@Tag(name = "EquipmentReportController", description = "统计报表")
@RestController
@RequestMapping("/erp/equipment/report/")
public class EquipmentReportController {

    @Autowired
    private EquipmentReportService equipmentReportService;

    @Autowired
    private EquipmentCategoryService equipmentCategoryService;
    @Autowired
    private DeptService deptService;

    @GetMapping("wxgs")
    @Operation(summary = "维修工时统计", description = "维修工时统计", method = "GET")
    public Result<List<EquipmentReportVO>> wxgs(@Parameter(description="查询参数") Criteria criteria) {
        List<EquipmentReportVO> list = equipmentReportService.getMaintenanceHours();
        return Result.OK(list);
    }

    @GetMapping("bygs")
    @Operation(summary = "保养工时统计", description = "保养工时统计", method = "GET")
    public Result<List<EquipmentReportVO>> bygs(@Parameter(description="查询参数") Criteria criteria) {
        List<EquipmentReportVO> list = equipmentReportService.getUpkeepHours();
        return Result.OK(list);
    }

    @GetMapping("byjh")
    @Operation(summary = "保养计划达成率", description = "保养计划达成率", method = "GET")
    public Result<List<EquipmentReportVO>> byjh(@Parameter(description="查询参数") Criteria criteria) {
        List<EquipmentReportVO> list = equipmentReportService.getUpkeepCompletionRate();
        return Result.OK(list);
    }

    @GetMapping("djjh")
    @Operation(summary = "点检计划达成率", description = "点检计划达成率", method = "GET")
    public Result<List<EquipmentReportVO>> djjh(@Parameter(description="查询参数") Criteria criteria) {
        List<EquipmentReportVO> list = equipmentReportService.getCheckCompletionRate();
        return Result.OK(list);
    }

    @GetMapping("jhgd")
    @Operation(summary = "稽核工单达成率", description = "稽核工单达成率", method = "GET")
    public Result<List<EquipmentReportVO>> jhgd(@Parameter(description="查询参数") Criteria criteria) {
        List<EquipmentReportVO> list = equipmentReportService.getAuditCompletionRate();
        return Result.OK(list);
    }

    @GetMapping("wxgd")
    @Operation(summary = "维修工单达成率", description = "维修工单达成率", method = "GET")
    public Result<List<EquipmentReportVO>> wxgd(@Parameter(description="查询参数") Criteria criteria) {
        List<EquipmentReportVO> list = equipmentReportService.getRepairCompletionRate();
        return Result.OK(list);
    }

    @GetMapping("sbgz")
    @Operation(summary = "设备故障统计报表", description = "设备故障统计报表", method = "GET")
    public Result<List<EquipmentReportVO>> sbgz(@Parameter(description="查询参数") Criteria criteria) {
        List<EquipmentReportVO> list = equipmentReportService.getEquipmentFailure();
        return Result.OK(list);
    }

    @GetMapping("sbzt")
    @Operation(summary = "设备状态看板", description = "设备状态看板", method = "GET")
    public Result<List<EquipmentReportVO>> sbzt(@Parameter(description="查询参数") Criteria criteria) {
        List<EquipmentReportVO> list = equipmentReportService.getEquipmentStatus();
        return Result.OK(list);
    }

    @GetMapping("sbwhl")
    @Operation(summary = "设备完好率", description = "设备完好率", method = "GET")
    public Result<List<EquipmentReportVO>> sbwhl(@Parameter(description="查询参数") Criteria criteria) {
        List<EquipmentReportVO> list = equipmentReportService.getEquipmentNormalStatus();
        return Result.OK(list);
    }

    @GetMapping("gztj")
    @Operation(summary = "设备故障次数统计", description = "设备故障次数统计", method = "GET")
    public Result<List<EquipmentReportVO>> gztj(@Parameter(description="查询参数") Criteria criteria) {
        List<EquipmentReportVO> list = equipmentReportService.getEquipmentFailureCount();
        return Result.OK(list);
    }

    @GetMapping("mtbf")
    @Operation(summary = "平均故障前时间MTBF", description = "平均故障前时间MTBF", method = "GET")
    public Result<List<EquipmentReportVO>> mtbf(@Parameter(description="查询参数") Criteria criteria) {
        List<EquipmentReportVO> list = equipmentReportService.getMeanTimeBeforeFailure();
        return Result.OK(list);
    }

    @GetMapping("mttr")
    @Operation(summary = "平均修复时间MTTR", description = "平均修复时间MTTR", method = "GET")
    public Result<List<EquipmentReportVO>> mttr(@Parameter(description="查询参数") Criteria criteria) {
        List<EquipmentReportVO> list = equipmentReportService.getMeanTimeToRepair();
        return Result.OK(list);
    }



    /*-------------------------------------------当月维修数据概览-----------------------------------------------------------*/


    @GetMapping("equNum")
    @Operation(summary = "设备数", description = "设备数", method = "GET")
    public Result<Integer> equipmentNum() throws Exception {
        // 调用服务层方法获取设备数量
        Integer equipmentNum = equipmentReportService.getEquipmentNum();
        return Result.OK(equipmentNum);
    }

    @GetMapping("badEquNum")
    @Operation(summary = "故障设备数", description = "故障设备数", method = "GET")
    public Result<Integer> badEquipmentNum() throws Exception {
        // 调用服务层方法获取故障设备数量
        Integer badEquipmentNum = equipmentReportService.getBadEquipmentNum();
        return Result.OK(badEquipmentNum);
    }

    @GetMapping("getMTTR")
    @Operation(summary = "平均修理时间MTTR", description = "平均修理时间MTTR", method = "GET")
    public Result<BigDecimal> getMTTR() throws Exception {
        // 调用服务层方法获取平均修理时间MTTR
        BigDecimal mttr = equipmentReportService.getMTTR();
        return Result.OK(mttr);
    }
    @GetMapping("getMTBF")
    @Operation(summary = "平均故障间隔MTBF", description = "平均故障间隔MTBF", method = "GET")
    public Result<BigDecimal> runtime() throws Exception {
        BigDecimal mtbf = equipmentReportService.getMTBF();
        return Result.OK(mtbf);
    }

    @GetMapping("equGood")
    @Operation(summary = "设备完好率", description = "设备完好率", method = "GET")
    public Result<BigDecimal> equGood() throws Exception{
        // 调用服务层方法获取设备完好率
        BigDecimal equGood = equipmentReportService.getEquGood();
        return Result.OK(equGood);
    }

    @GetMapping("aveResTime")
    @Operation(summary = "平均响应时间", description = "平均响应时间", method = "GET")
    public Result<String> averageResponseTime() throws Exception {
        // 调用服务层方法获取平均响应时间
        String averageResponseTime = equipmentReportService.getAverageResponseTime();
        return Result.OK(averageResponseTime);
    }

    @GetMapping("aveMaTime")
    @Operation(summary = "平均维修时间", description = "平均维修时间", method = "GET")
    public Result<String> averageMaintenanceTime() throws Exception{
        // 调用服务层方法获取平均维修时间
        String averageMaintenanceTime = equipmentReportService.getAverageMaintenanceTime();
        return Result.OK(averageMaintenanceTime);
    }

    @GetMapping("aveBadTime")
    @Operation(summary = "平均故障时长", description = "平均故障时长", method = "GET")
    public Result<String> averageDownTime() throws Exception{
        // 调用服务层方法获取平均故障时长
        String averageDownTime = equipmentReportService.getAverageDownTime();
        return Result.OK(averageDownTime);
    }

    /*-------------------------------------------------当月预防维护数据概览--------------------------------------------------------*/

    @GetMapping("pmPlanNum")
    @Operation(summary = "PM计划数", description = "PM计划数", method = "GET")
    public Result<Integer> pmPlanNum() throws Exception{
        // 从 equipmentReportService 中获取 PM 计划数量
        Integer pmPlanNum = equipmentReportService.getPMPlanNum();
        return Result.OK(pmPlanNum);
    }

    @GetMapping("pmActualNum")
    @Operation(summary = "PM实际数", description = "PM实际数", method = "GET")
    public Result<Integer> pmActualNum() throws Exception{
        // 从 equipmentReportService 中获取 PM 实际数量
        Integer pmActualNum = equipmentReportService.getPMActualNum();
        return Result.OK(pmActualNum);
    }

    @GetMapping("normp")
    @Operation(summary = "定期维护计划数", description = "定期维护计划数", method = "GET")
    public Result<Integer> normp() throws Exception{
        // 从 equipmentReportService 中获取 定期维护计划数量
        Integer normp = equipmentReportService.getNormp();
        return Result.OK(normp);
    }

    @GetMapping("norma")
    @Operation(summary = "定期维护实际数", description = "定期维护实际数", method = "GET")
    public Result<Integer> norma() throws Exception{
        // 从 equipmentReportService 中获取 定期维护实际数量
        Integer norma = equipmentReportService.getNorma();
        return Result.OK(norma);
    }

    @GetMapping("pmCom")
    @Operation(summary = "PM完成率", description = "PM完成率", method = "GET")
    public Result<String> pmCom() throws Exception{
        // 从 equipmentReportService 中获取 PM 完成率
        String pmCom = equipmentReportService.getPMCom();
        return Result.OK(pmCom);
    }

    @GetMapping("normCom")
    @Operation(summary = "定期维护完成率", description = "定期维护完成率", method = "GET")
    public Result<String> normCom() throws Exception{
        // 从 equipmentReportService 中获取 定期维护完成率
        String normCom = equipmentReportService.getNormCom();
        return Result.OK(normCom);
    }

    @GetMapping("onTimeCom")
    @Operation(summary = "按时完成率", description = "按时完成率", method = "GET")
    public Result<String> onTimeCom() throws Exception{
        // 从 equipmentReportService 中获取 按时完成率
        String onTimeCom = equipmentReportService.getOnTimeCom();
        return Result.OK(onTimeCom);
    }

    /**------------------------------------------------当月故障趋势------------------------------------------------------------*/

    @GetMapping("faultTrend")
    @Operation(summary = "当月故障趋势", description = "当月故障趋势", method = "GET")
    public Result<List<EquipmentReportVO>> faultTrend() throws Exception {
        List<EquipmentReportVO> faultTrendList = equipmentReportService.getFaultTrend();
        return Result.OK(faultTrendList);
    }
}
